//import java.sql.DriverManager;
//import java.sql.Connection;
import java.sql.*;
//import java.util.Properties;

//import java.io.InputStream;

public class Test1 {

	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false";
	static final String USER = "root";
	static final String PWD = "ZhouYang0128";
	
	public static void main(String[] args) throws Exception {
		/*
		// 创建properties对象
		Properties pro = new Properties();
		// 加载文件流
		InputStream in = Test1.class.getResourceAsStream("config.properties");
		// 类加载器，加载项目中的资源文件
		pro.load(in);
		
		String driver = pro.getProperty("driver");
		String url = pro.getProperty("url");
		String user = pro.getProperty("user");
		String pwd = pro.getProperty("pwd");
		
		// 把数据库的驱动包拷贝到项目中
		// 1、注册驱动
		Class.forName(driver); // 反射
		
		// 2、获取连接
		Connection conn = DriverManager.getConnection(url, user, pwd);
		
		// 测试打印对象
		System.out.println(conn);
		
		conn.close();
		*/
		
		Connection conn = null;
		Statement state = null;
		
		// 注册驱动
		Class.forName(JDBC_DRIVER);
		
		conn = DriverManager.getConnection(DB_URL, USER, PWD);
		
		state = conn.createStatement();
		String sql;
		sql = "SELECT * FROM users";
		//执行sql语句
		ResultSet res = state.executeQuery(sql);
		
		//打印结果
		while(res.next()) {
			int id = res.getInt("id");
			String name = res.getString("name");
			int age = res.getInt("age");
			String gender = res.getString("gender");
			
			System.out.print("ID: " + id);
			System.out.print(", NAME: " + name);
			System.out.print(", AGE: " + age);
			System.out.print(", GENDER: " + gender);
			System.out.print("\n");
		}
		
		//关闭连接
		res.close();
		state.close();
		conn.close();
		
	}

}
